package com.mars.foodserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mars.foodserver.constant.NumberConstant;
import com.mars.foodserver.entity.NotesLike;
import com.mars.foodserver.mapper.NotesLikeMapper;
import com.mars.foodserver.service.INotesLikeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wq
 * @since 2021-09-28
 */
@Service
public class NotesLikeServiceImpl extends ServiceImpl<NotesLikeMapper, NotesLike> implements INotesLikeService {

    @Override
    public void like(Integer userId, Integer notesId, Integer type) {
        if (type.equals(NumberConstant.ONE)) {
            //点赞
            NotesLike notesLike = baseMapper.selectOne(new LambdaQueryWrapper<NotesLike>().
                    eq(NotesLike::getUserId, userId).eq(NotesLike::getNotesId, notesId));
            if (Objects.isNull(notesLike)) {
                NotesLike like = new NotesLike();
                like.setNotesId(notesId);
                like.setUserId(userId);
                save(like);
            }
        } else {
            baseMapper.delete(new LambdaQueryWrapper<NotesLike>().
                    eq(NotesLike::getNotesId, notesId).eq(NotesLike::getUserId, userId));
        }
    }

    @Override
    public boolean isLikeNotes(Integer userId, Integer id) {
        NotesLike foodLike = baseMapper.selectOne(new LambdaQueryWrapper<NotesLike>().eq(NotesLike::getNotesId, id).
                eq(NotesLike::getUserId, userId));
        return Objects.nonNull(foodLike);
    }
}
